using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using MoreGen.Util;

namespace MoreGen.MapTypes
{
    public class Project
    {
        internal static Project CurrentSerial;
        public Project()
        {
            CurrentSerial = this;
        }

        public Database Database
        {
            get
            {
                return DataTypeMap.Databases[DBType];
            }
        }

        private string _DBType;
        [Category("Connection")]
        public string DBType
        {
            get { return _DBType; }
            set { _DBType = value; }
        }

        private string _ConnectionString = "Provider=SQLOLEDB;Data Source=05crosslaptop;Initial Catalog=Northwind;Integrated Security=SSPI;";
        [Category("Connection")]
        public string ConnectionString
        {
            get { return _ConnectionString; }
            set { _ConnectionString = value; }
        }

        private string _ProjectName;
        [Category("Project")]
        public string ProjectName
        {
            get { return _ProjectName; }
            set { _ProjectName = value; }
        }

        private string _FileName;
        [Category("Project")]
        public string FileName
        {
            get { return _FileName; }
            set { _FileName = value; }
        }

        private string _OutputDirectory;
        public string OutputDirectory
        {
            get { return _OutputDirectory; }
            set { _OutputDirectory = value; }
        }

        private string _SubDir;
        public string SubDir
        {
            get { return _SubDir; }
            set { _SubDir = value; }
        }
	

        private string _NameSpace;
        public string NameSpace
        {
            get { return _NameSpace; }
            set { _NameSpace = value; }
        }

        private string _Assembly;
        public string Assembly
        {
            get { return _Assembly; }
            set { _Assembly = value; }
        }

        private SerialDictionary<string, TableMap> _Tables = new SerialDictionary<string, TableMap>();
        [Browsable(false)]
        public SerialDictionary<string, TableMap> Tables
        {
            get { return _Tables; }
            set { _Tables = value; }
        }

        private List<string> _Sequences = new List<string>();
        public List<string> Sequences
        {
            get { return _Sequences; }
            set { _Sequences = value; }
        }
	

        public void Save()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Project));

            // Create an XmlTextWriter using a FileStream.
            Stream fs = new FileStream(FileName, FileMode.Create);
            XmlWriterSettings Settings = new XmlWriterSettings();
            Settings.Indent = true;
            Settings.NewLineHandling = NewLineHandling.Entitize;
            XmlWriter writer = XmlWriter.Create(fs, Settings);
            
            // Serialize using the XmlTextWriter.
            serializer.Serialize(writer, this);

            writer.Close();
            fs.Close();
        }

        public static Project Load(string FileName)
        {
            // Create an instance of the XmlSerializer specifying type and namespace.
            XmlSerializer serializer = new XmlSerializer(typeof(Project));

            // A FileStream is needed to read the XML document.
            FileStream fs = new FileStream(FileName, FileMode.Open);
            XmlReaderSettings Settings = new XmlReaderSettings();
            Settings.IgnoreWhitespace = true;
            Settings.IgnoreComments = true;
            XmlReader reader = XmlReader.Create(fs, Settings);

            try
            {
                Project p = (Project)serializer.Deserialize(reader); 

                // Use the Deserialize method to restore the object's state.
                return p;
            }
            finally
            {
                reader.Close();
                fs.Close();
            }
        }

        public override string ToString()
        {
            return ProjectName;
        }
    }
}
